//
//  MCAddCommunityViewController.swift
//  StoreManage
//
//  Created by MC on 2018/6/22.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCAddCommunityViewController: MCViewController {

    
    public var model = MCCommunityModel() {
        didSet {
            clerkId = model.clerkId.intValue
            communityId = model.communityId.intValue
            province = model.province
            city = model.city
            area = model.area
            isDefault = model.isDefault.intValue
            provinceView.textField.text = province + "/" + city + "/" + area
            communityView.textField.text = model.communityName
            detailView.textField.text = model.address
        }
    }
    
    public var clerkId = 0
    public var communityId = 0
    
    private var province = ""
    private var city = ""
    private var area = ""
    private var isDefault = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
    // MARK: - Custom Delegate
    
    // MARK: - Action
    @objc func rightItemClicked() {
        if (communityView.textField.text?.isEmpty)! {
            MCHUD_text(message: "请填写社区名称", view: self.view, delay: 1.2)
            return
        }
        
        if (provinceView.textField.text?.isEmpty)! {
            MCHUD_text(message: "请选择省市区", view: self.view, delay: 1.2)
            return
        }

        if (detailView.textField.text?.isEmpty)! {
            MCHUD_text(message: "请填写详细地址", view: self.view, delay: 1.2)
            return
        }

        let para = [
            "province" : province,
            "city" : city,
            "area" : area,
            "address" : detailView.textField.text ?? "",
            "clerkId" : clerkId,
            "communityName" : communityView.textField.text ?? "",
            "isDefault" : isDefault,
            "communityId" : communityId,
            ] as [String : Any]
        
        MCNetwork_post(url: JAVA_updateCommunityOfUser, para: para as [String : AnyObject], vc: self,hudSuperView: nil,isHiddenTabbar: true, success: { (response) in
            MCHUD_text(message: "配送社区设置成功", view: self.view, delay: 1.2)
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.2, execute: {
                self.navigationController?.popViewController(animated: true)
            })
            
        }, dislike: { (response) in }) { (error) in }
    }
    
    // MARK: - Network
    func sendNetworking() {
        
        let para = [
            "data" : "1",
            ] as [String : Any]
        
        MCNetwork_post(url: JAVA_getProvinceCityArea, para: para as [String : AnyObject], vc: self,hudSuperView: nil,isHiddenTabbar: true, success: { (response) in
            let dataArray = response["data"] as! NSArray
            
            
            
            for province in dataArray {
                let provinceDict =  province as! NSDictionary
                let  proName = provinceDict["proName"]
                self.provincesArrayM.add(proName ?? "")
                
                
                // 市
                let cityM = NSMutableArray()
                let areaM = NSMutableArray()
                let cityArray = provinceDict["citys"] as! NSArray
                for city in cityArray {
                    let cityDcit = city as! NSDictionary
                    cityM.add(cityDcit["cityName"] ?? "")
                    
                    
                    // 区
                    let areaSubM = NSMutableArray()
                    let areasArray = cityDcit["areas"] as! NSArray
                    for area in areasArray {
                        let areaDict = area as! NSDictionary
                        
                        areaSubM.add(areaDict["areaName"] ?? "")
                    }
                    areaM.add(areaSubM)
                }
                
                self.citysArrayM.add(cityM)
                self.areasArrayM.add(areaM)
            }
            
            self.shippingaddressView.provinces = self.provincesArrayM
            self.shippingaddressView.citys = self.citysArrayM
            self.shippingaddressView.areas = self.areasArrayM
            
        }, dislike: { (response) in }) { (error) in }
    }
    // MARK: - Method
    func baseSetting() {
        self.title = "新增配送社区"
        self.view.backgroundColor = UIColor.white
        
        
        navigationItem.rightBarButtonItem = MCSetBarButtonItem_text("保存", target: self, selector: #selector(rightItemClicked))
    }
    
    func initUI() {
        view.addSubview(communityView)
        communityView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(view)
            make.top.equalTo(view).offset(10)
            make.height.equalTo(40)
        }
        
        view.addSubview(provinceView)
        provinceView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(view)
            make.top.equalTo(communityView.snp.bottom).offset(10)
            make.height.equalTo(40)
        }

        
        view.addSubview(detailView)
        detailView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(view)
            make.top.equalTo(provinceView.snp.bottom).offset(10)
            make.height.equalTo(40)
        }

    }
    
    // MARK: - Setter & Getter
    lazy var communityView: MCAddTicketInputView = {
        let view = MCAddTicketInputView()
        view.titleLabel.text = "社区名称:"
        view.textField.placeholder = "请输入社区的名字"
        view.textField.delegate = self
        view.textField.returnKeyType = UIReturnKeyType.done
        return view
    }()
    
    lazy var detailView: MCAddTicketInputView = {
        let view = MCAddTicketInputView()
        view.titleLabel.text = "详细地址:"
        view.textField.placeholder = "不必重复填写省市区信息"
        view.textField.delegate = self
        view.textField.returnKeyType = UIReturnKeyType.done
        return view
    }()

    
    lazy var provinceView:ItemView = {
        let view = ItemView()
        view.titleLabel.text = "省  市  区:"
        view.textField.placeholder = "选择省市区"
        view.textField.delegate = self
        

        
        view.textField.inputView = shippingaddressView
        return view
    }()
    
    lazy var shippingaddressView: MCShippingAddressView = {
        let view = MCShippingAddressView()
        view.delegate = self
        view.frame = CGRect.init(x: 0, y: 0, width: 0, height: 216)
        return view
    }()
    
    lazy var provincesArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
    
    lazy var citysArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
    
    lazy var areasArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
}
extension MCAddCommunityViewController : UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return true
    }
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
      
        if textField == provinceView.textField {
            if provincesArrayM.count > 0 && citysArrayM.count > 0 && areasArrayM.count > 0 {
                
                
                let province = provincesArrayM[0] as? String ?? ""
                
                
                let cityArray = citysArrayM[0] as! NSArray
                let city = cityArray[0] as? String ?? ""
                
                
                let areaOneArray = areasArrayM[0] as! NSArray
                let areaTwoArray = areaOneArray[0] as! NSArray
                let area = areaTwoArray[0] as? String ?? ""
                
                
                self.province = province
                self.city = city
                self.area = area
                
                provinceView.textField.text = province + "/" + city + "/" + area
            }
        }
    }
}

extension MCAddCommunityViewController : MCShippingAddressViewDelegate {
    func MCShippingAddressViewSelected(province: String, city: String, area: String) {
        
        self.province = province
        self.city = city
        self.area = area
        
        self.provinceView.textField.text = province + "/" + city + "/" + area
    }
}
